package com.spc.car.common.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

public final class MapExtendUtils extends MapUtils {
    public MapExtendUtils() {
    }

    public static Map<String, Object> bean2Map(Object obj) {
        if (obj == null) {
            return null;
        } else {
            HashMap map = new HashMap();

            try {
                BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
                PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
                PropertyDescriptor[] var4 = propertyDescriptors;
                int var5 = propertyDescriptors.length;

                for(int var6 = 0; var6 < var5; ++var6) {
                    PropertyDescriptor property = var4[var6];
                    String key = property.getName();
                    if (!key.equals("class")) {
                        Method getter = property.getReadMethod();
                        Object value = getter.invoke(obj);
                        map.put(key, value);
                    }
                }
            } catch (Exception var11) {
                System.out.println("bean2Map Error " + var11);
            }

            return map;
        }
    }

    public static Map<String, String> bean2StringMap(Object obj, boolean ignoreNull) {
        Map<String, Object> result = bean2Map(obj);
        if (isEmpty(result)) {
            return MapUtils.EMPTY_MAP;
        } else {
            Map<String, String> target = new HashMap();
            Iterator var4 = result.entrySet().iterator();

            while(var4.hasNext()) {
                Entry<String, Object> entry = (Entry)var4.next();
                Object value = entry.getValue();
                if (null == value) {
                    if (!ignoreNull) {
                        target.put(entry.getKey(), "");
                    }
                } else {
                    target.put(entry.getKey(), value instanceof String ? (String)value : value.toString());
                }
            }

            return target;
        }
    }

    public static Map<String, Object> filterMap(Map<String, Object> sourceMap, String[] includeKeys, String[] excludeKeys) {
        if (MapUtils.isEmpty(sourceMap)) {
            return null;
        } else {
            Map<String, Object> targetMap = new HashMap();
            List excludeList;
            if (ArrayUtils.isNotEmpty(includeKeys)) {
                excludeList = Arrays.asList(includeKeys);
                sourceMap.forEach((key, val) -> {
                    if (excludeList.contains(key)) {
                        if (val instanceof Map) {
                            val = filterMap((Map)val, includeKeys, excludeKeys);
                        }

                        targetMap.put(key, val);
                    }

                });
            } else {
                if (!ArrayUtils.isNotEmpty(excludeKeys)) {
                    return sourceMap;
                }

                excludeList = Arrays.asList(excludeKeys);
                sourceMap.forEach((key, val) -> {
                    if (!excludeList.contains(key)) {
                        if (val instanceof Map) {
                            val = filterMap((Map)val, includeKeys, excludeKeys);
                        }

                        targetMap.put(key, val);
                    }

                });
            }

            return targetMap;
        }
    }

    public static List<Map<String, Object>> filterListMap(List<Map<String, Object>> sourceListMap, String idKeyName, String[] includes, String[] excludes) {
        List<Map<String, Object>> areaMapList = new ArrayList();
        sourceListMap.forEach((a) -> {
            areaMapList.add(filterMap(a, includes, excludes));
        });
        return removeRepeatValue(areaMapList, idKeyName);
    }

    public static List<Map<String, Object>> removeRepeatValue(List<Map<String, Object>> sourceList, String keyName) {
        Map<String, Map<String, Object>> msp = new HashMap();
        List<Map<String, Object>> targetList = new ArrayList();

        String key;
        for(int i = 0; i < sourceList.size(); ++i) {
            Map<String, Object> map = (Map)sourceList.get(i);
            key = (String)map.get(keyName);
            map.remove(keyName);
            msp.put(key, map);
        }

        Set<String> mspKey = msp.keySet();
        Iterator var9 = mspKey.iterator();

        while(var9.hasNext()) {
            key = (String)var9.next();
            if (key != null) {
                Map<String, Object> newMap = (Map)msp.get(key);
                newMap.put(keyName, key);
                targetList.add(newMap);
            }
        }

        return targetList;
    }

    public static BigDecimal sumValue(List<Map<String, Object>> sourceList, String key) {
        BigDecimal amount = BigDecimal.ZERO;
        if (CollectionUtils.isNotEmpty(sourceList) && StringUtils.isNotBlank(key)) {
            amount = (BigDecimal)sourceList.stream().map((a) -> {
                Object obj = a.get(key);
                BigDecimal value;
                if (obj instanceof BigDecimal) {
                    value = (BigDecimal)obj;
                } else if (obj instanceof Double) {
                    value = BigDecimal.valueOf((Double)obj);
                } else {
                    value = new BigDecimal(obj.toString());
                }

                return value;
            }).reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        return amount;
    }

    public static Map<String, String> bean2StringMap(Object obj) {
        Map<String, Object> result = bean2Map(obj);
        if (MapUtils.isEmpty(result)) {
            return MapUtils.EMPTY_MAP;
        } else {
            Map<String, String> target = new HashMap();
            Iterator var3 = result.entrySet().iterator();

            while(var3.hasNext()) {
                Entry<String, Object> entry = (Entry)var3.next();
                Object value = entry.getValue();
                target.put(entry.getKey(), value instanceof String ? (String)value : value.toString());
            }

            return target;
        }
    }
}
